<?php

/**
 * Impress - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Impress
 * @package Impress
 */

/**
 * This class provides access to data layer.
 *
 * @package Impress
 * @subpackage Models
 */

class ADS_DataSourceItemModel extends ADS_DefaultModel {
    
    private $content;
    private $type;
    
    public function __construct($content = 'default', $type = 'item') {
        $this->content = $content;
        $this->type    = $type;
    }
    
    public function countItems($sourceId) {
        if(!is_int($sourceId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_datasource_items] AS t1
          INNER JOIN
            [:wp:posts] AS t2 ON t1.targetId = t2.ID
          WHERE
            t1.sourceId = %i', $sourceId, '
            %if', (bool)$this->useFilter, 'AND %and', $this->filter, '
          ORDER BY
            t1.position
        ')->cnt;
    }
    
    public function getItems($sourceId) {
        if(!is_int($sourceId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        $result = false;
        switch($this->type){
            case 'item':
                $result = dibi::fetchAll('
                    SELECT
                        t1.itemId,
                        t1.duration,
                        t1.position,
                        t1.validFrom,
                        t1.validTo,
                        t2.post_title AS name,
                        t2.guid
                    FROM
                        [:wp:ads_datasource_items] AS t1
                    INNER JOIN
                        [:wp:posts] AS t2 ON t1.targetId = t2.ID
                    WHERE
                        t1.sourceId = %i', $sourceId, 
                    '%if', (bool)$this->useFilter, 'AND %and', $this->filter,
                    '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy,
                    '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset
                );
                break;
            case 'tag':
                $result = dibi::fetchAll('
                    SELECT
                        t1.itemId,
                        t1.duration,
                        t1.position,
                        t1.validFrom,
                        t1.validTo,
                        t1.targetId,
                        t2.name
                    FROM
                        [:wp:ads_datasource_items] AS t1
                    INNER JOIN
                        [:wp:terms] AS t2 ON t1.targetId = t2.term_id
                    WHERE
                        t1.sourceId = %i', $sourceId, 
                    '%if', (bool)$this->useFilter, 'AND %and', $this->filter,
                    '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy,
                    '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset
                );
                break;
        }        
        
        return $result;
    }
    
    public function addItem($item){
        if(!is_array($item)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //format date
        $item['validFrom%t'] = $item['validFrom'];unset($item['validFrom']);
        $item['validTo%t']   = $item['validTo'];unset($item['validTo']);
        //get next position
        $item['position'] = ((int)dibi::fetch('
            SELECT
                MAX(position) as position
            FROM
                [:wp:ads_datasource_items]
            WHERE
                sourceId = %i
        ', $item['sourceId'])->position)+1;
        //insert
        dibi::query('INSERT INTO [:wp:ads_datasource_items]', $item);
    }
    
    public function removeItem($itemId){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get item
        $item = dibi::fetch('SELECT * FROM [:wp:ads_datasource_items] WHERE itemId = %i',$itemId);
        //update positions
        dibi::query('
            UPDATE 
                [:wp:ads_datasource_items] 
            SET 
                position = position - 1 
            WHERE 
                sourceId = %i 
            AND 
                position > %i
        ', $item->sourceId, $item->position);
        //remove item
        dibi::query('DELETE FROM [:wp:ads_datasource_items] WHERE itemId = %i LIMIT 1', $itemId);
    }
    
    public function increaseItemPosition($itemId){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get item
        $item = dibi::fetch('SELECT * FROM [:wp:ads_datasource_items] WHERE itemId = %i',$itemId);
        //update item on next position
        dibi::query('
            UPDATE 
                [:wp:ads_datasource_items] 
            SET 
                position = position - 1 
            WHERE 
                sourceId = %i 
            AND 
                position = %i + 1
        ', $item->sourceId, $item->position);
        //update item
        dibi::query('
            UPDATE 
                [:wp:ads_datasource_items] 
            SET 
                position = position + 1 
            WHERE 
                itemId = %i 
        ', $itemId);
    }
    
    public function decreaseItemPosition($itemId){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get item
        $item = dibi::fetch('SELECT * FROM [:wp:ads_datasource_items] WHERE itemId = %i',$itemId);
        //update item on next position
        dibi::query('
            UPDATE 
                [:wp:ads_datasource_items] 
            SET 
                position = position + 1 
            WHERE 
                sourceId = %i 
            AND 
                position = %i - 1
        ', $item->sourceId, $item->position);
        //update item
        dibi::query('
            UPDATE 
                [:wp:ads_datasource_items] 
            SET 
                position = position - 1 
            WHERE 
                itemId = %i 
        ', $itemId);
    }
    
    public function editItem($itemId, $item){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        dibi::query('UPDATE [:wp:ads_datasource_items] SET', $item, 'WHERE itemId = %i', $itemId);
    }
    
    public function getItem($itemId) {
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        
        $result = false;
        
        switch($this->type){
            
            case 'item':
                $result = dibi::fetch('
                    SELECT
                        t1.itemId,
                        t1.duration,
                        t1.position,
                        t1.validFrom,
                        t1.validTo,
                        t2.post_title AS name,
                        t2.guid
                    FROM
                        [:wp:ads_datasource_items] AS t1
                    INNER JOIN
                        [:wp:posts] AS t2 ON t1.targetId = t2.ID
                    WHERE
                        t1.itemId = %i', $itemId
                );
                break;
            
            case 'tag':
                $result = dibi::fetch('
                    SELECT
                        t1.itemId,
                        t1.duration,
                        t1.position,
                        t1.validFrom,
                        t1.validTo,
                        t2.name
                    FROM
                        [:wp:ads_datasource_items] AS t1
                    INNER JOIN
                        [:wp:terms] AS t2 ON t1.targetId = t2.term_id
                    WHERE
                        t1.itemId = %i', $itemId
                );
                break;
        }        
        
        return $result;
        
    }
}
